package org.jeecg.modules.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.shop.entity.Catalog;
import org.jeecg.modules.shop.entity.Company;
import org.jeecg.modules.shop.entity.Designer;
import org.jeecg.modules.shop.entity.Discount;
import org.jeecg.modules.shop.mapper.CatalogMapper;
import org.jeecg.modules.shop.mapper.CompanyMapper;
import org.jeecg.modules.shop.mapper.DesignerMapper;
import org.jeecg.modules.shop.mapper.DiscountMapper;
import org.jeecg.modules.shop.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 公司表
 * @Author: jeecg-boot
 * @Date:   2023-10-17
 * @Version: V1.0
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private DesignerMapper designerMapper;
	@Autowired
	private DiscountMapper discountMapper;
	@Autowired
	private CatalogMapper catalogMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Company company, List<Designer> designerList,List<Discount> discountList) {
		Catalog catalog = catalogMapper.selectById(company.getCatelogId());
		if (Objects.nonNull(catalog)) {
			company.setCatelogName(catalog.getName());
		}
		companyMapper.insert(company);
		if(designerList!=null && designerList.size()>0) {
			for(Designer entity:designerList) {
				//外键设置
				entity.setCompanyId(company.getId());
				designerMapper.insert(entity);
			}
		}
		if(discountList!=null && discountList.size()>0) {
			for(Discount entity:discountList) {
				//外键设置
				entity.setCompanyId(company.getId());
				discountMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Company Company,List<Designer> aDesignerList,List<Discount> aDiscountList) {
		Catalog catalog = catalogMapper.selectById(Company.getCatelogId());
		if (Objects.nonNull(catalog)) {
			Company.setCatelogName(catalog.getName());
		}
		companyMapper.updateById(Company);

		//1.先删除子表数据
		designerMapper.deleteByMainId(Company.getId());
		discountMapper.deleteByMainId(Company.getId());

		//2.子表数据重新插入
		if(aDesignerList!=null && aDesignerList.size()>0) {
			for(Designer entity:aDesignerList) {
				//外键设置
				entity.setCompanyId(Company.getId());
				designerMapper.insert(entity);
			}
		}
		if(aDiscountList!=null && aDiscountList.size()>0) {
			for(Discount entity:aDiscountList) {
				//外键设置
				entity.setCompanyId(Company.getId());
				discountMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(Integer id) {
		designerMapper.deleteByMainId(id);
		discountMapper.deleteByMainId(id);
		companyMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			designerMapper.deleteByMainId(Integer.valueOf(id.toString()));
			discountMapper.deleteByMainId(Integer.valueOf(id.toString()));
			companyMapper.deleteById(id);
		}
	}


}
